﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace Extractor.LibCode
{
    public enum DICTIOARY_TYPE
    {
        MATCH_EVENT,
        POSITION,
        REGIONID,
        COMPETITION_TYPE,
        GROUP_MATCH,
        ROUND_DSC,
        NONE

    }
    public static class cCommon
    {

        public static DataTable initTable(string[] fieldname)
        {

            DataTable dt = new DataTable();
            foreach (string s in fieldname)
            {
                dt.Columns.Add(s);
            }
            return dt;
        }

        public static string DictionaryType(DICTIOARY_TYPE dtyp)
        {
            int ie = (int)dtyp;
            switch (ie)
            {
                case 0:
                    return "MATCH-EVENT";
                case 1:
                    return "POSITION";
                case 2:
                    return "REGIONID";
                case 3:
                    return "COMPETITIION-TYPE";
                case 4:
                    return "GRUPMATCH";
                case 5:
                    return "ROUNDDSC";
                case 6:
                    return "";

                default:

                    return "";
            }
        }
        public static void kotakpesan(string s)
        {
            MessageBox.Show(s);
        }
        public static void fillCombobox(ComboBox cb, string textmember, string valuemember, DICTIOARY_TYPE dty)
        {

            DataTable dt = new DataTable();
            cb.Items.Clear();
            cDBService odb = new cDBService();

            if (odb.getComboboxData(ref dt, "TBDICTIONARY", valuemember, textmember, dty))
            {
                cb.DisplayMember = textmember;
                cb.ValueMember = valuemember;
                cb.DataSource = dt;
            }

        }

        public static void fillCombobox(ComboBox cb, string textmember, string valuemember, string tablename)
        {
            DataTable dt = new DataTable();
            cb.Items.Clear();
            cDBService odb = new cDBService();

            if (odb.getComboboxData(ref dt, tablename, valuemember, textmember, DICTIOARY_TYPE.NONE))
            {
                cb.DisplayMember = textmember;
                cb.ValueMember = "svalue";//valuemember;
                
                
               // dt.Rows.InsertAt(dt.NewRow(), 0);
                
                cb.DataSource = dt;
            }
            //cb.Items.Insert(0, "");

        }
        public static string[] trimLine(string[] sline)
        {
            StringBuilder sb = new StringBuilder();
            List<string> ss = new List<string>();
            string stemp;
            foreach (string s in sline)
            {
                stemp = s.Trim();
                if (stemp != string.Empty)
                {
                    //sb.AppendLine(s.Trim());
                    ss.Add(stemp);
                }
            }
            //string[] ss=stemp.ToArray();
            return ss.ToArray();

        }
        public static string strimLine(string[] sline)
        {
            StringBuilder sb = new StringBuilder();
            List<string> ss = new List<string>();
            string stemp;
            foreach (string s in sline)
            {
                stemp = s.Trim();
                if (stemp != string.Empty)
                {
                    sb.AppendLine(stemp);

                }
            }
            //string[] ss=stemp.ToArray();
            return sb.ToString();

        }
        public static void selectall(object o, KeyEventArgs k)
        {
            if (k.Control & k.KeyCode == Keys.A)
                ((TextBox)o).SelectAll();
        }

        public static string[] getArraystring(TextBox tb)
        {
            return tb.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        }
        public static void ClearTB(TextBox[] arrTB)
        {
            foreach (TextBox tb in arrTB)
            {
                tb.Text = string.Empty;
            }

        }
        public static List<int> AllIndexesOf(this string str, string value)
        {
            if (String.IsNullOrEmpty(value))
                throw new ArgumentException("the string to find may not be empty", "value");
            List<int> indexes = new List<int>();
            for (int index = 0; ; index += value.Length)
            {
                index = str.IndexOf(value, index);
                if (index == -1)
                    return indexes;
                indexes.Add(index);
            }
        }

    }

}
